<?php
class ProcedureRequirementsController extends AppController {

	var $name = 'ProcedureRequirements';
	var $helpers = array('Html', 'Form', 'Ajax', 'Javascript');
	var $uses = array('ProcedureRequirement', 'Procedure');

	function index() {
		$this->ProcedureRequirement->recursive = 0;
		$this->set('procedureRequirements', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid ProcedureRequirement.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('procedureRequirement', $this->ProcedureRequirement->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->ProcedureRequirement->create();
			if ($this->ProcedureRequirement->save($this->data)) {
				$this->flash(__('ProcedureRequirement saved.', true), array('action'=>'index'));
				$this->_stop();
			} else {
			}
		}
		$procedures = $this->ProcedureRequirement->Procedure->find('list');
		$requirements = $this->ProcedureRequirement->Requirement->find('list');
		$this->set(compact('procedures', 'requirements'));
	}


	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ProcedureRequirement', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->ProcedureRequirement->save($this->data)) {
				$this->Session->setFlash(__('The ProcedureRequirement has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The ProcedureRequirement could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProcedureRequirement->read(null, $id);
		}
		$stepRequirements = $this->ProcedureRequirement->StepRequirement->find('list');
		$procedures = $this->ProcedureRequirement->Procedure->find('list');
		$requirements = $this->ProcedureRequirement->Requirement->find('list');
		$this->set(compact('stepRequirements','procedures','requirements'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ProcedureRequirement', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProcedureRequirement->del($id)) {
			$this->Session->setFlash(__('ProcedureRequirement deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	function link() {
		if (!empty($this->params['form']['TAB_requirementsDispos'])) {
			foreach ($this->params['form']['TAB_requirementsDispos'] as $index => $requirement)
			{

				$this->ProcedureRequirement->create();
				$id_proc = $this->Procedure->find(array('proc_reference' => $this->data['Procedure']['proc_reference'], 'proc_project' => $this->Session->read('id_projet')), array('proc_index'));
				
				$this->data['ProcedureRequirement']['pra_proc'] = $id_proc['Procedure']['proc_index'] ;
				$this->data['ProcedureRequirement']['pra_requirement'] = $requirement ;
				if (!$this->ProcedureRequirement->save($this->data)) {
					$this->flash(__('Erreur ! Recommencez SVP.', true), array('controller' => 'requirements', 'action'=>'link'));
				}
			}
			$this->flash(__('Requirement(s) associé(s) avec succès', true), array('controller' => 'requirements', 'action'=>'link'));
		}

//		$this->set('walle', $this);
		
//		$this->flash(__('Erreur ! Recommencez SVP.', true), array('controller' => 'requirements', 'action'=>'link'));
	}


	function unlink($id = null) {
		if (!empty($this->params['form']['TAB_requirementsAssocies']) && !empty($this->data['Procedure']['proc_index'])) {
			foreach ($this->params['form']['TAB_requirementsAssocies'] as $index => $requirement)
			{
				$id_PR = $this->ProcedureRequirement->find(array('pra_proc' => $this->data['Procedure']['proc_index'], 'pra_requirement' => $requirement), array('pra_index'));
					
				echo "PR ".$id_PR['ProcedureRequirement']['pra_index'];
				print_r($id_PR) ;
				if ($id_PR){
					if (! $this->ProcedureRequirement->del($id_PR['ProcedureRequirement']['pra_index'])) {
						$this->flash(__('Erreur ! Recommencez SVP.', true), array('controller' => 'requirements', 'action'=>'link'));
					}
				}
			}
			$this->flash(__('Requirement(s) désassocié(s) avec succès', true), array('controller' => 'requirements', 'action'=>'link'));
		}

//		$this->flash(__('Erreur ! Recommencez SVP.', true), array('controller' => 'requirements', 'action'=>'link'));

//		$this->set('walle', $this);
		
	}

}
?>